package team.lourd.network.system.config;


import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import team.lourd.network.system.annotation.JsonArg;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.lang.reflect.Type;

/** 
* @Description: JsonPathArgumentResolver 用于解析Controll中的Json数据
* @Author: Lourd 
* @Date: 2019/1/17 
*/
@Slf4j
public class JsonPathArgumentResolver implements HandlerMethodArgumentResolver {

    private static final String JSONBODYATTRIBUTE = "JSON_REQUEST_BODY";

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(JsonArg.class);
    }

    /** 
    * @Description: 解析数据
    * @Param: [parameter, mavContainer, webRequest, binderFactory] 
    * @return: java.lang.Object 
    * @Author: Lourd
    * @Date: 2019/1/17 
    */
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        // 获取json数据
        JSONObject jsonBody = (JSONObject) servletRequest.getAttribute(JSONBODYATTRIBUTE);
        System.out.println(jsonBody);
        // json数据为空
        if (jsonBody == null) {
            jsonBody = getJsonFromBody(servletRequest);
        }
        System.out.println(jsonBody);
        // 转为JSONObject
        servletRequest.setAttribute(JSONBODYATTRIBUTE, jsonBody);
        parameter = parameter.nestedIfOptional();
        Type targetType = parameter.getNestedParameterType();
        Class<?> targetClass = (targetType instanceof Class ? (Class<?>) targetType : null);
        System.out.println(targetClass);
        JsonArg ja = parameter.getParameterAnnotation(JsonArg.class);
        System.out.println(ja.value());
        JSONObject jsonObject = jsonBody.getJSONObject(ja.value());
        log.info("JsonPathArgumentResolver Success");
        // String val = JsonPath.read(body, parameter.getMethodAnnotation(JsonArg.class).value());
        return JSONObject.toBean(jsonObject, targetClass);
    }


    /**
    * @Description: 从 HttpServletRequest中获取数据
    * @Param: [request]
    * @return: net.sf.json.JSONObject
    * @Author: Lourd
    * @Date: 2019/1/17
    */
    private JSONObject getJsonFromBody(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.fromObject(sb.toString());
    }
}